package cn.net.yugu.doraemon.sharding.strategy.impl;

import cn.net.yugu.doraemon.sharding.model.ShardContext;
import cn.net.yugu.doraemon.sharding.strategy.ITableShardStrategy;
import cn.net.yugu.doraemon.sharding.utils.MurMurHashUtils;

/**
 * hash 然后根据tableSize 取余.
 * @author wanghongli
 * @date 2023/12/16 14:56
 * @description HashTableShardStrategy
 **/
public class HashedTableShardStrategy implements ITableShardStrategy {
    @Override
    public String generateTableName(String tableNamePrefix, ShardContext shardContext) {
        verifyTableNamePrefix(tableNamePrefix);
        String hashedValue = String.valueOf(shardContext.getShardValue());
        long val = MurMurHashUtils.hashUnsigned(hashedValue);
        return (tableNamePrefix + "_" + (val % shardContext.getTableSize())).intern();
    }
}
